{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "051d3bd3-0b75-45c7-80be-4f8bfd3c306e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "source:  ./BEPT/downstreamData/test_time_images\n",
      "patch_save_dir:  ./test_time_images/patch_splits/patches\n",
      "mask_save_dir:  ./test_time_images/patch_splits/masks\n",
      "stitch_save_dir:  ./test_time_images/patch_splits/stitches\n",
      "source : ./BEPT/downstreamData/test_time_images\n",
      "save_dir : ./test_time_images/patch_splits\n",
      "patch_save_dir : ./test_time_images/patch_splits/patches\n",
      "mask_save_dir : ./test_time_images/patch_splits/masks\n",
      "stitch_save_dir : ./test_time_images/patch_splits/stitches\n",
      "{'seg_params': {'seg_level': -1, 'sthresh': 8, 'mthresh': 7, 'close': 4, 'use_otsu': False, 'keep_ids': 'none', 'exclude_ids': 'none'}, 'filter_params': {'a_t': 100, 'a_h': 16, 'max_n_holes': 8}, 'patch_params': {'use_padding': True, 'contour_fn': 'four_pt'}, 'vis_params': {'vis_level': -1, 'line_thickness': 250}}\n",
      "================== ['TCGA-3C-AALK-01Z-00-DX1.svs']\n",
      "\n",
      "\n",
      "progress: 0.00, 0/1\n",
      "processing TCGA-3C-AALK-01Z-00-DX1.svs\n",
      "Creating patches for:  TCGA-3C-AALK-01Z-00-DX1 ...\n",
      "Total number of contours to process:  4\n",
      "Bounding Box: 10080 53824 8833 8993\n",
      "Contour Area: 39714816.0\n",
      "Extracted 695 coordinates\n",
      "Bounding Box: 6400 40768 17281 9985\n",
      "Contour Area: 58214400.0\n",
      "Extracted 1023 coordinates\n",
      "Bounding Box: 11808 8512 13313 26913\n",
      "Contour Area: 77876736.0\n",
      "Extracted 1181 coordinates\n",
      "Bounding Box: 11584 2880 81249 70273\n",
      "Contour Area: 3382077440.0\n",
      "Extracted 50616 coordinates\n",
      "start stitching TCGA-3C-AALK-01Z-00-DX1\n",
      "original size: 95488 x 81920\n",
      "downscaled size for stiching: 2984 x 2560\n",
      "number of patches: 53515\n",
      "patch size: 224x224 patch level: 0\n",
      "ref patch size: (224, 224)x(224, 224)\n",
      "downscaled patch size: 7x7\n",
      "progress: 0/53515 stitched\n",
      "progress: 5352/53515 stitched\n",
      "progress: 10704/53515 stitched\n",
      "progress: 16056/53515 stitched\n",
      "progress: 21408/53515 stitched\n",
      "progress: 26760/53515 stitched\n",
      "progress: 32112/53515 stitched\n",
      "progress: 37464/53515 stitched\n",
      "progress: 42816/53515 stitched\n",
      "progress: 48168/53515 stitched\n",
      "segmentation took 0.5852844715118408 seconds\n",
      "patching took 2.138701915740967 seconds\n",
      "stitching took 2.9228696823120117 seconds\n",
      "average segmentation time in s per slide: 0.5852844715118408\n",
      "average patching time in s per slide: 2.138701915740967\n",
      "average stiching time in s per slide: 2.9228696823120117\n"
     ]
    }
   ],
   "source": [
    "%run create_patches_fp.py \\\n",
    "--source ./BEPT/downstreamData/test_time_images  \\\n",
    "--save_dir ./test_time_images/patch_splits \\\n",
    "--patch_size 224 \\\n",
    "--seg \\\n",
    "--patch \\\n",
    "--stitch\n",
    "# --preset tcga.csv \\"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "13cfbba7-d11d-4d4b-809f-0ad5e2127c69",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os \n",
    "import pandas as pd \n",
    "\n",
    "df = pd.read_csv('./test_time_images/patch_splits/process_list_autogen.csv') # 这个是上一步生成的文件\n",
    "ids1 = [i[:-4] for i in df.slide_id]\n",
    "ids2 = [i[:-3] for i in os.listdir('./test_time_images/patch_splits/patches/')]\n",
    "df['slide_id'] = ids1\n",
    "ids = df['slide_id'].isin(ids2)\n",
    "sum(ids)\n",
    "df.loc[ids].to_csv('./test_time_images/patch_splits/Step_2.csv',index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "574e54fa-5777-430a-8ef2-183459cbeca5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "initializing dataset\n",
      "Loads checkpoint by local backend from path: ./BEPT/Model/mmselfsup/TCGA_Checkpoints/beitv2_backbone_cancer.pth\n",
      "The model and loaded state dict do not match exactly\n",
      "\n",
      "unexpected key in source state_dict: backbone.mask_token, backbone.rel_pos_bias.relative_position_bias_table, backbone.rel_pos_bias.relative_position_index\n",
      "\n",
      "missing keys in source state_dict: backbone.layers.0.attn.relative_position_bias_table, backbone.layers.0.attn.relative_position_index, backbone.layers.1.attn.relative_position_bias_table, backbone.layers.1.attn.relative_position_index, backbone.layers.2.attn.relative_position_bias_table, backbone.layers.2.attn.relative_position_index, backbone.layers.3.attn.relative_position_bias_table, backbone.layers.3.attn.relative_position_index, backbone.layers.4.attn.relative_position_bias_table, backbone.layers.4.attn.relative_position_index, backbone.layers.5.attn.relative_position_bias_table, backbone.layers.5.attn.relative_position_index, backbone.layers.6.attn.relative_position_bias_table, backbone.layers.6.attn.relative_position_index, backbone.layers.7.attn.relative_position_bias_table, backbone.layers.7.attn.relative_position_index, backbone.layers.8.attn.relative_position_bias_table, backbone.layers.8.attn.relative_position_index, backbone.layers.9.attn.relative_position_bias_table, backbone.layers.9.attn.relative_position_index, backbone.layers.10.attn.relative_position_bias_table, backbone.layers.10.attn.relative_position_index, backbone.layers.11.attn.relative_position_bias_table, backbone.layers.11.attn.relative_position_index, backbone.ln2.weight, backbone.ln2.bias, head.fc.weight, head.fc.bias\n",
      "\n",
      "loading model checkpoint\n",
      "\n",
      "progress: 0/1\n",
      "TCGA-3C-AALK-01Z-00-DX1\n",
      "downsample [1. 1.]\n",
      "downsampled_level_dim [95488 81920]\n",
      "level_dim [95488 81920]\n",
      "name TCGA-3C-AALK-01Z-00-DX1\n",
      "patch_level 0\n",
      "patch_size 224\n",
      "save_path ./test_time_images/patch_splits/patches\n",
      "\n",
      "feature extraction settings\n",
      "target patch size:  None\n",
      "pretrained:  True\n",
      "transformations:  Compose(\n",
      "    ToTensor()\n",
      "    Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))\n",
      ")\n",
      "processing ./test_time_images/patch_splits/patches/TCGA-3C-AALK-01Z-00-DX1.h5: total of 23 batches\n",
      "batch 0/23, 0 files processed\n",
      "batch 20/23, 48000 files processed\n",
      "\n",
      "computing features for ./test_time_images/test_time_FEATURES_DIRECTORY/h5_files/TCGA-3C-AALK-01Z-00-DX1.h5 took 1611.7145047187805 s\n",
      "features size:  (53515, 768)\n",
      "coordinates size:  (53515, 2)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "%run extract_features_fp_beit.py \\\n",
    "--data_h5_dir ./test_time_images/patch_splits/ \\\n",
    "--data_slide_dir ./BEPT/downstreamData/test_time_images \\\n",
    "--csv_path ./test_time_images/patch_splits/Step_2.csv \\\n",
    "--feat_dir ./test_time_images/test_time_FEATURES_DIRECTORY \\\n",
    "--batch_size 2400 \\\n",
    "--slide_ext .svs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73ffd0bd-1ad5-4b5c-8ff5-b31d9803332f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mmselfsup_yzc",
   "language": "python",
   "name": "mmselfsup_yzc"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
